import Vue from 'vue'
import Vuex from 'vuex'
import http from '@/utils/request/index.js'
import {
	getUserInfo
} from '../utils/getStronge.js';
import {  queryByIdHrRoot, erpOmcPlatformAccountFinalUpdateStatus, queryPermissionsByUser} from '@/api/index.js'
import {getTaskInputUserType,getTaskInputDataStatus} from '@/api/taskInput/index.js'
Vue.use(Vuex)

export default new Vuex.Store({
	state: {
		versionInfo: {}, //版本信息
		userInfo: getUserInfo(), //用户信息
		permissionList: [],
		indexList: [],// 首页信息模块控制
		buttonAuth: [], // 按钮权限控制
		getDictItemsCode: {}, // 获取数据字典
		selectPerson: [], // 公告管理选中的人员
	},
	mutations: {
		SET_VERSION_INFO(state,playod){
			state.versionInfo = playod;
		},
		SET_USERINFO(state, playod){
			state.userInfo = playod
		},
		SET_PERMISSIONLIST: (state, permissionList) => {
		  state.permissionList = permissionList
		},
		// 首页信息模块控制
		setIndexList (state,playod) {
			state.indexList = playod;
		},
		setButtonAuth (state,playod) {
			state.buttonAuth = playod;
		},
		setSelectPerson (state,playod) {
			state.selectPerson = playod;
		},
		
		// 获取数据字典
		UI_CACHE_DB_DICT_DATA (state,playod) {
			state.getDictItemsCode = playod;
		}
	},
	actions: {
		// 获取各类的信息接口
		async getLoadTreeRoot({commit},params){
			let res = await queryByIdHrRoot(params)
			if (res.success && Array.isArray(res.result) ) {
				return res
			}
		},
		// 状态修改为变更中
		async getUpdateStatus({commit},code){
			let res = await erpOmcPlatformAccountFinalUpdateStatus({code})
			if (res.result) {
				return res
			}
		},
		setVersionInfo({commit}, playod){
			commit('SET_VERSION_INFO', playod);
		},
		//设置用户信息
		setUserInfo({
			commit
		}, playod){
			commit('SET_USERINFO', playod);
		},
    // 获取用户权限
    GetPermissionList({ commit }) {
		return new Promise((resolve, reject) => {
		  queryPermissionsByUser().then(response => {
			const menuData = response.result.menu;
			const authData = response.result.auth;
			const allAuthData = response.result.allAuth;
			uni.setStorageSync('LOGIN_USER_BUTTON_AUTH',JSON.stringify(authData));
			uni.setStorageSync('SYS_BUTTON_AUTH',JSON.stringify(allAuthData));
			if (menuData && menuData.length > 0) {
			  menuData.forEach((item, index) => {
				if (item["children"]) {
				  let hasChildrenMenu = item["children"].filter((i) => {
					return !i.hidden || i.hidden == false
				  })
				  if (hasChildrenMenu == null || hasChildrenMenu.length == 0) {
					item["hidden"] = true
				  }
				}
			  })
			  commit('SET_PERMISSIONLIST', menuData)
			} else {
			  reject('getPermissionList: permissions must be a non-null array !')
			}
			resolve(response)
		  }).catch(error => {
			reject(error)
		  })
		})
	  },
		// 设置首页信息模块列表
		async actionsSetIndexList ({commit}, playod) {
			let res = await http.get('/api/erpmng/sys/permission/getUserAppPermissionByToken')
			if (res.success) {
				uni.removeStorageSync('setButtonAuth')
				uni.removeStorageSync('setIndexList')
				commit('setIndexList', res.result.appMenu);
				commit('setButtonAuth', res.result.buttonAuth)
				uni.setStorageSync('setButtonAuth',JSON.stringify(res.result.buttonAuth));
				uni.setStorageSync('setIndexList',JSON.stringify(res.result.appMenu));
				return res
			}
		},
		// 获取数据字典
		async getDictItemsFromCache ({commit}, playod) {
			let res = await http.get('/api/erpmng/sys/dict/queryAllDictItems')
			if (res.success) {
				uni.setStorageSync('getDictItemsCode', res.result)
				commit('UI_CACHE_DB_DICT_DATA', res.result);
			}
		},
		// 设置选中发文管理的人员
		async actionsSetSelectPerson ({commit}, playod) {
			commit('setSelectPerson', playod && playod.length?playod:[]);
		},
		// 必须补填工时才可以进入系统
		async checkMustFillHours() { 
		  let aa = await getTaskInputUserType()
		  // 不需要补填工时
		  if (aa.result.type === 'white') {
			return 
		  }
		  let res = await getTaskInputDataStatus({thisWeek:'fill_in'})
		  if(res.success){
			return res.result
		  }
	
		},
	}
})
